package com.qiantu.phone.ui.activity;

import android.view.View;
import android.widget.LinearLayout;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hjq.http.listener.HttpCallback;
import com.qiantu.api.entity.PanelBindConfig;
import com.qiantu.api.entity.PanelBindStatusBean;
import com.qiantu.api.http.LLHttpManager;
import com.qiantu.api.http.model.HttpData;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.manager.ActivityManager;
import com.qiantu.phone.ui.adapter.PanelBindOperationAdapter;

import java.util.List;
import java.util.Map;

public class PanelBindOperationActivity extends AppActivity {
    public String coreId;
    private RecyclerView list;
    private PanelBindOperationAdapter adapter;
    private String panelSerialNo;
    private List<PanelBindConfig> bingingDatas;
    private int bingingCount = 0;
    private int checkCount = 0;
    private LinearLayout linearLayout;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_panel_qrbinging;
    }

    @Override
    protected void initView() {
        linearLayout = findViewById(R.id.bottom_operate);
        getTitleBar().getRightView().setText("");
        list = findViewById(R.id.room_recycler_view);
        list.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter = new PanelBindOperationAdapter(getContext());
        list.setAdapter(adapter);
        setOnClickListener(R.id.btn_retry,R.id.btn_finish);
    }

    @Override
    protected void initData() {
        bingingDatas = (List<PanelBindConfig>) getIntent().getSerializableExtra("bingingDatas");
        adapter.setData(bingingDatas);
        bingingData();
    }

    protected void bingingData(){
        PanelBindConfig config = bingingDatas.get(bingingCount);
        if(config.getSerialNo() != null){
            LLHttpManager.cancelPanelConfig(this, config, new HttpCallback<HttpData<PanelBindConfig>>(this) {
                @Override
                public void onSucceed(HttpData<PanelBindConfig> result) {
                    super.onSucceed(result);
                    if (result.getData() != null) {
                        //拿到CoreId后开始轮询等待服务器返回绑定成功 10秒后失败 3秒请求一次
                        checkCount = 0;
                        coreId = config.getBindSerialNo();
                        postDelayed(pollingRunnable, 2000);//2秒后开始轮询
                        bingingDatas.get(bingingCount).setBindStatus(1);
                        adapter.notifyDataSetChanged();
                    }else{
                        showFailView();
                    }
                }
                @Override
                public void onFail(Exception e) {
                    showFailView();
                    super.onFail(e);
                }
            });
        }else{
            LLHttpManager.panelBind(this, config, new HttpCallback<HttpData<PanelBindConfig>>(this) {
                @Override
                public void onSucceed(HttpData<PanelBindConfig> result) {
                    super.onSucceed(result);
                    if (result.getData() != null) {
                        //拿到CoreId后开始轮询等待服务器返回绑定成功 10秒后失败 3秒请求一次
                        checkCount = 0;
                        postDelayed(pollingRunnable, 2000);//2秒后开始轮询
                        coreId = result.getData().getSerialNo();
                        bingingDatas.get(bingingCount).setBindStatus(1);
                        adapter.notifyDataSetChanged();
                    }else{
                        showFailView();
                    }
                }
                @Override
                public void onFail(Exception e) {
                    showFailView();
                    super.onFail(e);
                }
            });
        }

    }

    private final Runnable pollingRunnable = new Runnable() {
        @Override
        public void run() {
            requestInitQuery(coreId);
            postDelayed(pollingRunnable, 2000);//2秒后开始轮询
        }
    };

    public void requestInitQuery(String coreId) {
        PanelBindConfig config = bingingDatas.get(bingingCount);
        int type = config.getSerialNo() == null ? 1 : 0;
        LLHttpManager.checkPanelBindingStatus(this, type, coreId, new HttpCallback<HttpData<PanelBindStatusBean>>(this) {
            @Override
            public void onSucceed(HttpData<PanelBindStatusBean> result) {
                super.onSucceed(result);
                if (result.getData() != null) {
                    int state = result.getData().status;
                    switch (state) {
                        case 0://继续轮询等待
                            if(checkCount > 2){
                                removeCallbacks(pollingRunnable);
                                showFailView();
                            }else{
                                checkCount++;
                            }
                            break;
                        case 1://绑定成功
                            removeCallbacks(pollingRunnable);
                            bingingSuccess();
                            break;
                    }
                }
            }

            @Override
            public void onFail(Exception e) {
                super.onFail(e);
            }
        });
    }

    protected void bingingSuccess(){
        PanelBindConfig config = bingingDatas.get(bingingCount);
        bingingDatas.get(bingingCount).setBindStatus(2);
        bingingCount++;
        if(bingingCount >= bingingDatas.size()){
            //绑定结束
            finish();
            ActivityManager.getInstance().finishActivity(PanelBindDeviceActivity.class);//返回上上层
            ActivityManager.getInstance().finishActivity(KGXPanelBindDeviceActivity.class);
        }else{
            bingingData();
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_retry:
                linearLayout.setVisibility(View.GONE);
                bingingData();
                break;
            case R.id.btn_finish:
                ActivityManager.getInstance().finishActivity(PanelBindDeviceActivity.class);
                ActivityManager.getInstance().finishActivity(KGXPanelBindDeviceActivity.class);
                finish();
                break;
            default: {

            }
        }
    }

    protected void showFailView(){
        linearLayout.setVisibility(View.VISIBLE);
        bingingDatas.get(bingingCount).setBindStatus(3);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onRightClick(View view) {

    }
}
